﻿@using CatalogModel
@model List<object>

@{
	ViewBag.Title = "Index";
	Layout = "~/Views/Shared/_Layout.cshtml";
	var type = (Type)ViewBag.Type;
}

<h2>@type.Name</h2>

<p>
	@Html.ActionLink("Create New", "Create", new { tn = type.Name })
</p>
<table class="table">
	<tr>
		@foreach (var item in type.GetProperties())
		{
			//if (item.PropertyType.Namespace.Contains("CatalogModel") || item.PropertyType.Namespace.Contains("System") || item.PropertyType.Name == "UnitType")
			{
				<th class="vertical">
					<div>@Html.DisplayName(item.Name)</div>
				</th>
			}
		}
		<th width="5%"></th>
	</tr>

	@foreach (var item in Model)
	{
		var itemID = type.GetProperty("ID").GetValue(item);

		<tr>
			@foreach (var prop in type.GetProperties())
			{
				var value = type.GetProperty(prop.Name).GetValue(item);

				if (prop.PropertyType.Namespace.Contains("Codelists"))
				{
					<td>
						@{
							if (value == null)
							{
								@Html.Label("")
							}
							else
							{
								var val = (ICodelist)value;
								@Html.Label(val.ID.ToString(), htmlAttributes: new { @title = val.ShortDescription })
							}
						}
					</td>
							}
							else if (prop.PropertyType.Namespace.Contains("System") || prop.PropertyType.Name == "UnitType")
							{
								<td>
									@Html.Label((value == null) ? "" : value.ToString())
								</td>
							}
							else
							{
								string vall = "";
								if (value != null)
								{
									vall = value.GetType().GetProperty(prop.Name).GetValue(value).ToString();
								}
								<td>
									@Html.Label(vall)
								</td>
							}
				}
<td>
				@Html.ActionLink("Edit", "Edit", new { id = itemID, tn = type.Name })
				@Html.ActionLink("Details", "Details", new { id = itemID, tn = type.Name })
				@Html.ActionLink("Delete", "Delete", new { id = itemID, tn = type.Name })
			</td>
		</tr>
							}

</table>
